﻿package com.badibadi.mail;

import java.util.ArrayList;
import java.util.List;

import com.badibadi.activity.BaseActivity;
import com.badibadi.activity.SelectPictsMail_Activity;
import com.badibadi.infos.ImageAttribute;
import com.badibadi.mail.MailSendAMassMailActivity.MySpSizeAdapter;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.SinaExpressionActivity;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

//站内信这个界面只有一个activity继承它  AddApplyFormActivity
public class MailActivity extends BaseActivity implements OnClickListener {
	public static final int ALLMAIL=123123;//用他来查找所有关于站内信的
	private static final int BiaoQing = 8081;
	private static final int PICTURE = 1;
	private static final String TAG = "MailActivity";
	private ImageButton btn_textSize_Setting, btn_chuxi, btn_xiezhi,
			btn_xiahuaxian, btn_biaoqin, btn_movies;
	protected ImageButton btn_paizhao;
	private Intent intent = null;
	private String[] ssp = { "5", "10", "15", "20", "25", "30", "35", "40",
			"45", "50", "60", "70" };
	protected EditText activity_mail_content;
	protected TextView activity_mail_send, title_mail;
	protected ImageView activity_mail_titalimg;
	protected Button activity_mail_back;

	ListView lv;// 文字大小列表
	/**
	 * 加粗 TextView tv = (TextView)findViewById(R.id.TextView01); TextPaint tp =
	 * tv.getPaint(); tp.setFakeBoldText(true);
	 * 
	 * login_registration.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //添加线
	 * //login_edit_username.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
	 * //删除线
	 */
	/**
	 * 是否是粗体
	 */
	private boolean isChuti = false;
	/**
	 * 是否是斜体
	 */
	private boolean isXieti = false;
	/**
	 * 是否有下划线
	 */
	private boolean hasXiahuaxian = false;
	AlertDialog textdialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.activity_mail);
		init();
	}

	public void init() {
		// TODO Auto-generated method stub
		btn_textSize_Setting = (ImageButton) this
				.findViewById(R.id.mail_textSize_Setting);
		btn_textSize_Setting.setOnClickListener(MailActivity.this);
		btn_chuxi = (ImageButton) this.findViewById(R.id.mail_chuxi);
		btn_chuxi.setOnClickListener(MailActivity.this);
		btn_xiezhi=(ImageButton)this.findViewById(R.id.mail_xie_zhi);
		btn_xiezhi.setOnClickListener(this);
		btn_xiahuaxian=(ImageButton)this.findViewById(R.id.mail_xiahuaxian);
		btn_xiahuaxian.setOnClickListener(this);
		
		activity_mail_content = (EditText) findViewById(R.id.activity_mail_content);
		activity_mail_send = (TextView) findViewById(R.id.activity_mail_send);
		activity_mail_titalimg = (ImageView) findViewById(R.id.activity_mail_titalimg);
		activity_mail_back = (Button) findViewById(R.id.activity_mail_back);
		activity_mail_back.setOnClickListener(this);
		
	
		
		btn_paizhao = (ImageButton) findViewById(R.id.mail_paizhao);
		btn_paizhao.setOnClickListener(this);
		title_mail = (TextView) findViewById(R.id.title_mail);
		this.findViewById(R.id.mail_biaoqing).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.mail_textSize_Setting:
			initDialog();
			break;
		case R.id.mail_chuxi:
			ChangeChuti();
			break;
		case R.id.mail_xiahuaxian:
			ChangeXiaHuaXian();
			break;
		case R.id.mail_xie_zhi:
			ChangeXieti();
			break;
		case R.id.activity_mail_back:
			finish();
			break;
		case R.id.mail_paizhao:
			Intent intent = new Intent(MailActivity.this,
					SelectPictsMail_Activity.class);
			intent.putExtra("panduan", "no_set");
			startActivityForResult(intent, PICTURE);
			break;
		case R.id.mail_biaoqing:
			Intent intent2 = new Intent(MailActivity.this,
					SinaExpressionActivity.class);
			startActivityForResult(intent2, BiaoQing);
			break;
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		if (arg0 == BiaoQing
				&& arg1 == SinaExpressionActivity.ACTIVITY_BIAOQING) {
			if (arg2 != null) {
//				String con1 = activity_mail_content.getText().toString();
//				String con2 = arg2.getStringExtra("result");
//				activity_mail_content.setText(con1 + con2);
//				activity_mail_content.setSelection((con1 + con2).length());
				String content2 = arg2
						.getStringExtra("result");
				int index=activity_mail_content.getSelectionStart();
				Editable edit=activity_mail_content.getEditableText();
				edit.insert(index, content2);
			}

		}
		super.onActivityResult(arg0, arg1, arg2);
	}

	/**
	 * 设置斜体和取消斜体
	 */
	private void ChangeXieti() {
	
		if (isXieti == false) {
//			   
			SpannableString msp = new SpannableString(activity_mail_content
					.getText().toString().trim());
			msp.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0,
					msp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 斜体
			// activity_mail_content.
			// activity_mail_content.getPaint().setFakeBoldText(true);

			activity_mail_content.setText(msp);
			int index=msp.length();
					activity_mail_content.setSelection(index);
			isXieti = true;
		} else {
//			   
			SpannableString msp = new SpannableString(activity_mail_content
					.getText().toString().trim());
			msp.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), 0,
					msp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 正常
			// activity_mail_content.getPaint().setFakeBoldText(false);
			activity_mail_content.setText(msp);
			int index=msp.length();
					activity_mail_content.setSelection(index);
			isXieti = false;
		}
		
		isChuti=false;
		hasXiahuaxian=false;
		
	}

	/**
	 * 设置字体下划线
	 */
	private void ChangeXiaHuaXian() {
		if (hasXiahuaxian == false) {
//			   
			SpannableString msp = new SpannableString(activity_mail_content
					.getText().toString().trim());
			msp.setSpan(new UnderlineSpan(), 0, msp.length(),
					Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			// activity_mail_content.
			// activity_mail_content.getPaint().setFakeBoldText(true);

			activity_mail_content.setText(msp);
					int index=msp.length();
							activity_mail_content.setSelection(index);
			hasXiahuaxian = true;
		} else {
//			   
			SpannableString msp = new SpannableString(activity_mail_content
					.getText().toString().trim());
			msp.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), 0,
					msp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 正常
			// activity_mail_content.getPaint().setFakeBoldText(false);
			activity_mail_content.setText(msp);
			int index=msp.length();
					activity_mail_content.setSelection(index);
			hasXiahuaxian = false;
		}
		isChuti=false;
		isXieti=false;
	}

	/**
	 * 设置粗体和取消粗体
	 */
	private void ChangeChuti() {
		if (isChuti == false) {
			SpannableString msp = new SpannableString(activity_mail_content
					.getText().toString().trim());
			msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0,
					msp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 粗体
			// activity_mail_content.
			// activity_mail_content.getPaint().setFakeBoldText(true);

			activity_mail_content.setText(msp);
			int index=msp.length();
			activity_mail_content.setSelection(index);
			isChuti = true;
		} else {
			SpannableString msp = new SpannableString(activity_mail_content
					.getText().toString().trim());
			msp.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), 0,
					msp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 正常
			// activity_mail_content.
			// activity_mail_content.getPaint().setFakeBoldText(true);

			activity_mail_content.setText(msp);
			int index=msp.length();
			activity_mail_content.setSelection(index);
			isChuti = false;
		}

		hasXiahuaxian=false;
		isXieti=false;
	}

	private void initDialog() {
		lv = new ListView(MailActivity.this);
		// lv.setChoiceMode(choiceMode);
		// ArrayAdapter<Integer> arr=new
		// ArrayAdapter<Integer>(getApplicationContext(), resource,
		// textViewResourceId, objects)
		final MySpSizeAdapter spAdapter = new MySpSizeAdapter();
		lv.setAdapter(spAdapter);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				setTextSize(Float.parseFloat((String) spAdapter
						.getItem(position)));
				if (textdialog != null) {
					textdialog.dismiss();
				}

			}
		});
		textdialog = new AlertDialog.Builder(MailActivity.this)
				.setTitle(getResources().getString(R.string.l_xb90)).setView(lv).show();
	}

	/**
	 * 设置字体大小
	 * 
	 * @param content
	 */
	private void setTextSize(Float content) {
		activity_mail_content.setTextSize(content);
	}

	public class MySpSizeAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return ssp.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return ssp[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			LayoutInflater inflater = LayoutInflater.from(MailActivity.this);
			convertView = inflater
					.inflate(R.layout.mail_sp_import_layout, null);

			TextView tv = (TextView) convertView
					.findViewById(R.id.mail_import_sp);
			tv.setText("" + ssp[position]);
			tv.setTextSize(Integer.parseInt(ssp[position]));
			// Typeface face=Typeface.createFromAsset(getAssets(),
			// "founts/cukaitijian.ttf");
			// tv.setTypeface(face);
			// Paint p=new Paint();
			// p.setTextSkewX(-1);
			return convertView;
		}

	}
}
